package com.kyzh.core.pager.weal.accountbuy

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.beans.Deal
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.adapters.MyDealAdapter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import kotlinx.android.synthetic.main.fragment_mydeal.*
import kotlinx.android.synthetic.main.titlebar.*
import org.jetbrains.anko.support.v4.toast

class MyDealFragment : BaseFragment() {


    private lateinit var context: Activity
    private var titles = mutableListOf<String>()
//    private var titles = arrayListOf(getString(R.string.all), getString(R.string.pendingReview), getString(R.string.selling1), getString(R.string.sold1), getString(R.string.bought))
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_mydeal, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        context = requireActivity()
        initView()
        initData()
    }

    private fun initView() {
        titles.add(getString(R.string.all))
        titles.add(getString(R.string.pendingReview))
        titles.add(getString(R.string.selling1))
        titles.add(getString(R.string.sold1))
        titles.add(getString(R.string.bought))
        viewPager.adapter = ViewPagerAdapter()
        tabLayout.setupWithViewPager(viewPager)
        viewPager.currentItem = 0
        close.setOnClickListener { requireActivity().finish() }
        tvTitle.text = getString(R.string.myOrder)
    }

    private fun initData() {

    }


    private inner class ViewPagerAdapter() : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return `object` == view
        }

        override fun getCount(): Int {
            return titles.size
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = LayoutInflater.from(context).inflate(R.layout.recyclerview, null)

            val root = view.findViewById<SmartRefreshLayout>(R.id.root)
            val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
            val array = arrayListOf<Deal>()
            recyclerView.layoutManager = object : LinearLayoutManager(context) {
                override fun canScrollHorizontally(): Boolean {
                    return false
                }
            }
            val adapter = MyDealAdapter(R.layout.mydeal_item, array)
            recyclerView.adapter = adapter
            root.setEnableAutoLoadMore(false)
            root.autoRefresh()
            var p1 = 1
            var max1 = 1

            root.setOnRefreshListener {
                WealImpl.myDeal(position, 1, object : ResultListener {
                    override fun success(beans: Any, p: Int, max: Int) {
                        array.clear()
                        array.addAll(beans as ArrayList<Deal>)
                        p1 = p
                        max1 = max
                        root.finishRefresh()
                        root.finishLoadMore()
                        adapter.notifyDataSetChanged()
                    }

                    override fun error() {
                        adapter.setEmptyView(R.layout.empty)
                        root.finishRefresh()
                        root.finishLoadMore()
                    }
                })
            }
            root.setOnLoadMoreListener {
                if (p1 > max1) {
                    toast(getString(R.string.noHaveMore))
                    root.finishLoadMore()
                } else {
                    WealImpl.myDeal(position, p1, object : ResultListener {
                        override fun success(beans: Any, p: Int, max: Int) {
                            array.addAll(beans as ArrayList<Deal>)
                            p1 = p
                            max1 = max
                            root.finishRefresh()
                            root.finishLoadMore()
                            adapter.notifyDataSetChanged()
                        }

                        override fun error() {
                            root.finishRefresh()
                            root.finishLoadMore()
                        }
                    })
                }
            }

            container.addView(view)
            return view
        }


        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titles[position]
        }

    }
}
